﻿using NoteHelper.BaseControl;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NoteHelper.Utils
{
    public static class ControlExt
    {
        /// <summary>
        /// 把容器中所有以pre开头的控件的值转成Hashtable
        /// </summary>
        public static Hashtable GetData(this Control container, string pre)
        {
            var ht = new Hashtable();
            var lst = FindAllControls(container, pre);
            int l = pre.Length;
            foreach (Control control in lst)
            {
                string key = control.Name.Substring(l);
                var name = control.GetType().Name;
                var helper = Helper4Factory.Instance().GetControlHelper(name);
                ht.Add(key, helper.GetValue(control));
            }
            return ht;
        }

        /// <summary>
        /// 用Hashtable给容器中所有以pre开头的控件赋值
        /// </summary>
        public static void SetData(this Control container, string pre, Hashtable ht)
        {
            var lst = FindAllControls(container, pre);
            int l = pre.Length;
            foreach (Control control in lst)
            {
                string key = control.Name.Substring(l);
                var value = ht.GetValue(key);
                var name = control.GetType().Name;
                var helper = Helper4Factory.Instance().GetControlHelper(name);
                helper.SetValue(control, value);
            }
        }

        public static object GetValue(this Hashtable ht, object key)
        {
            if (ht == null || !ht.ContainsKey(key))
                return null;

            return ht[key];
        }

        public static void GetValueFormDataRow(this Hashtable ht, DataRow row)
        {
            foreach (DataColumn column in row.Table.Columns)
            {
                if (ht.ContainsKey(column.ColumnName))
                {
                    ht[column.ColumnName] = row[column];
                }
            }
        }

        public static void ClearContext(Control container, string pre)
        {
            var lst = FindAllControls(container, pre);
            foreach (Control control in lst)
            {
                if (control is SourceGrid.Grid grid)
                {
                    grid.Rows.Clear();
                }
                else
                {
                    control.Text = "";
                }
            }
        }

        public static void ChangeInputState(this Control container, string pre, bool enabled)
        {
            var lst = FindAllControls(container, pre);
            foreach (Control control in lst)
            {
                if (control is SourceGrid.Grid grid)
                {
                    //grid.Rows.Clear();
                }
                else
                {
                    control.Enabled = enabled;
                }
            }
        }

        public static List<Control> FindAllControls(Control container, string pre)
        {
            var lst = new List<Control>();
            int count = container.Controls.Count;
            for (int i = 0; i < count; i++)
            {
                var control = container.Controls[i];
                if (control.Name.StartsWith(pre))
                    lst.Add(control);
                if (control.HasChildren)
                {
                    lst.AddRange(FindAllControls(control, pre));
                }
            }

            return lst;
        }

        public static string DisplayCurrentContext(DataSet ds)
        {
            var ctxBuilder = new StringBuilder();
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                var table = ds.Tables[i];
                ctxBuilder.Append("-----------------------------------------------");
                ctxBuilder.AppendFormat("\r\nTable{0}-{1}:", i, table.TableName);

                int columCount = table.Columns.Count;
                foreach (DataRow row in table.Rows)
                {
                    for (int j = 0; j < columCount; j++)
                    {
                        ctxBuilder.AppendFormat("\r\ncol{0}:{1}", table.Columns[j].ColumnName, row[j]);
                    }
                }
                ctxBuilder.Append("\r\n-----------------------------------------------");
            }
            return ctxBuilder.ToString();
        }
    }
}
